import json

from flask_cors import CORS

from apps import create_app
from apps import socketio
# from threading import Lock
from flask import Flask, render_template, session, request, copy_current_request_context, jsonify

# from flask_socketio import SocketIO, Namespace, emit, join_room, leave_room, \
#     close_room, rooms, disconnect
#
# from apps.GPS.gpsService import recordGpsAndPath
# from apps.path.pathService import pathIsExist
# from variance import global_dict

app = create_app()
CORS(app, supports_credentials=True)

# if __name__ != '__main__':
#     gunicorn_logger = logging.getLogger('gunicorn.error')
#     app.logger.handlers = gunicorn_logger.handlers
#     app.logger.setLevel(gunicorn_logger.level)


@app.route('/')
def index():
    return render_template('index.html', async_mode=socketio.async_mode)


# 硬登录接口
@app.route('/api/login/account', methods=["POST"])
def login():
    data = json.loads(request.get_data())
    username = data['username']
    password = data['password']
    msg_type = data['type']
    from apps.gpsAndPathUtils import deal_cors
    if password == 'ant.design' and username == 'admin':
        return deal_cors(jsonify({
            'status': 'ok',
            'type': msg_type,
            'currentAuthority': 'admin',
        }))


# 硬退出登录接口


# 查询当前用户接口
@app.route('/api/currentUser', methods=["GET"])
def current_user():
    from apps.gpsAndPathUtils import deal_cors
    return deal_cors(jsonify({
        'success': True,
        'data': {
            'name': 'Serati Ma',
            'avatar': 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png',
            'userid': '00000001',
            'email': 'antdesign@alipay.com',
            'signature': '海纳百川，有容乃大',
            'title': '交互专家',
            'group': '蚂蚁金服－某某某事业群－某某平台部－某某技术部－UED',
            'tags': [
                {
                    'key': '0',
                    'label': '很有想法的',
                },
                {
                    'key': '1',
                    'label': '专注设计',
                },
                {
                    'key': '2',
                    'label': '辣~',
                },
                {
                    'key': '3',
                    'label': '大长腿',
                },
                {
                    'key': '4',
                    'label': '川妹子',
                },
                {
                    'key': '5',
                    'label': '海纳百川',
                },
            ],
            'notifyCount': 12,
            'unreadCount': 11,
            'country': 'China',
            'geographic': {
                'province': {
                    'label': '浙江省',
                    'key': '330000',
                },
                'city': {
                    'label': '杭州市',
                    'key': '330100',
                },
            },
            'address': '西湖区工专路 77 号',
            'phone': '0752-268888888',
        },
    }))


if __name__ == '__main__':
    CORS(app, supports_credentials=True)
    socketio.run(app, host="0.0.0.0", port=81)
